Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 365)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.48125 11.52593 11.56999 11.61345 11.65632 11.69862 11.74037 11.78159
##   [9] 11.82229 11.86248 11.90219 11.94144 11.98023 12.01859 12.05653 12.09407
##  [17] 12.13121 12.16790 12.20410 12.23979 12.27493 12.30949 12.34342 12.37670
##  [25] 12.40929 12.44116 12.47226 12.50308 12.53397 12.56473 12.59515 12.62503
##  [33] 12.65416 12.68234 12.70936 12.73501 12.76042 12.78664 12.81335 12.84020
##  [41] 12.86688 12.89305 12.91840 12.94259 12.96529 12.98618 13.00493 13.02121
##  [49] 13.03724 13.05490 13.07334 13.09172 13.10920 13.12493 13.13807 13.14778
##  [57] 13.15321 13.15491 13.15412 13.15103 13.14582 13.13869 13.12983 13.11943
##  [65] 13.10768 13.09476 13.08086 13.06618 13.05091 13.03523 13.01487 12.98693
##  [73] 12.95362 12.91719 12.87986 12.84387 12.81145 12.78482 12.75949 12.73004
##  [81] 12.69733 12.66222 12.62558 12.58826 12.55112 12.51503 12.48086 12.44945
##  [89] 12.42168 12.39841 12.37791 12.35793 12.33851 12.31969 12.30152 12.28404
##  [97] 12.26728 12.25131 12.23615 12.22185 12.20846 12.19602 12.18457 12.17415
## [105] 12.16532 12.15840 12.15308 12.14906 12.14604 12.14371 12.14177 12.13991
## [113] 12.13783 12.13667 12.13758 12.14022 12.14429 12.14946 12.15542 12.16184
## [121] 12.16841 12.17481 12.18071 12.18581 12.18977 12.19431 12.20092 12.20900
## [129] 12.21794 12.22712 12.23594 12.24379 12.25005 12.25413 12.25601 12.25633
## [137] 12.25545 12.25371 12.25145 12.24903 12.24679 12.24508 12.24425 12.24465
## [145] 12.24662 12.25051 12.25487 12.25823 12.26086 12.26305 12.26509 12.26725
## [153] 12.26984 12.27312 12.27740 12.28294 12.29005 12.29900 12.31007 12.32356
## [161] 12.34030 12.36036 12.38287 12.40697 12.43182 12.45654 12.48028 12.50217
## [169] 12.52136 12.54163 12.56690 12.59649 12.62974 12.66598 12.70455 12.74478
## [177] 12.78601 12.82756 12.86878 12.90899 12.94753 12.98373 13.01693 13.04646
## [185] 13.07166 13.09185 13.10637 13.11456 13.11840 13.12032 13.12036 13.11851
## [193] 13.11480 13.10923 13.10183 13.09260 13.08156 13.06873 13.05411 13.03773
## [201] 13.01959 12.99972 12.97315 12.93655 12.89234 12.84293 12.79077 12.73826
## [209] 12.68784 12.64192 12.60293 12.56554 12.52342 12.47749 12.42867 12.37786
## [217] 12.32600 12.27400 12.22279 12.17326 12.12636 12.08299 12.04408 12.00639
## [225] 11.96665 11.92565 11.88423 11.84319 11.80335 11.76553 11.73054 11.69920
## [233] 11.66952 11.63922 11.60868 11.57829 11.54842 11.51946 11.49179 11.46579
## [241] 11.44183 11.42031 11.40161 11.38610 11.37240 11.35914 11.34665 11.33529
## [249] 11.32543 11.31740 11.31158 11.30832 11.30796 11.31140 11.31893 11.33000
## [257] 11.34407 11.36059 11.37902 11.39882 11.41944 11.44034 11.46097 11.48079
## [265] 11.49925 11.52168 11.55221 11.58856 11.62845 11.66958 11.70966 11.74641
## [273] 11.77754 11.80077 11.81949 11.83858 11.85793 11.87742 11.89694 11.91637
## [281] 11.93561 11.95453 11.97304 11.99101 12.00832 12.02488 12.04056 12.05525
## [289] 12.06790 12.07785 12.08563 12.09176 12.09674 12.10109 12.10533 12.10998
## [297] 12.11554 12.12255 12.13150 12.14292 12.15553 12.16773 12.17962 12.19127
## [305] 12.20277 12.21420 12.22565 12.23719 12.24891 12.26089 12.27321 12.28596
## [313] 12.29921 12.31306 12.32961 12.35021 12.37372 12.39902 12.42499 12.45050
## [321] 12.47444 12.49569 12.51310 12.52830 12.54355 12.55876 12.57383 12.58868
## [329] 12.60320 12.61729 12.63088 12.64385 12.65612 12.66758 12.67815 12.68773
## [337] 12.69665 12.70526 12.71351 12.72132 12.72863 12.73536 12.74147 12.74687
## [345] 12.75165 12.75595 12.75975 12.76304 12.76584 12.76812 12.76990 12.77116
## [353] 12.77190 12.77211 12.77180 12.77095 12.76958 12.76768 12.76526 12.76233
## [361] 12.75888 12.75493 12.75047 12.74551 12.74006
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 365)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.75355 10.84132 10.92743 11.01185 11.09457 11.17557 11.25485 11.33238
##   [9] 11.40816 11.48216 11.55438 11.62481 11.69341 11.76019 11.82513 11.88820
##  [17] 11.94943 12.00883 12.06645 12.12231 12.17645 12.22889 12.27966 12.32880
##  [25] 12.37634 12.42230 12.46673 12.50928 12.54969 12.58812 12.62470 12.65956
##  [33] 12.69284 12.72469 12.75524 12.78463 12.81201 12.83659 12.85863 12.87838
##  [41] 12.89611 12.91205 12.92648 12.93964 12.95179 12.96319 12.97409 12.98474
##  [49] 12.99245 12.99511 12.99389 12.98994 12.98442 12.97848 12.97329 12.97000
##  [57] 12.96976 12.97131 12.97257 12.97348 12.97401 12.97409 12.97369 12.97274
##  [65] 12.97121 12.96903 12.96617 12.96257 12.95819 12.95297 12.94714 12.94090
##  [73] 12.93414 12.92678 12.91872 12.90985 12.90008 12.88931 12.87744 12.86451
##  [81] 12.85064 12.83595 12.82057 12.80460 12.78817 12.77141 12.75441 12.73732
##  [89] 12.72024 12.70330 12.68611 12.66819 12.64956 12.63024 12.61022 12.58952
##  [97] 12.56816 12.54613 12.52345 12.50013 12.47618 12.45162 12.42644 12.40066
## [105] 12.36923 12.32891 12.28238 12.23236 12.18153 12.13259 12.08826 12.05121
## [113] 12.02416 12.00221 11.97905 11.95525 11.93140 11.90808 11.88585 11.86531
## [121] 11.84702 11.83157 11.81953 11.81148 11.80800 11.80984 11.81665 11.82733
## [129] 11.84077 11.85590 11.87160 11.88679 11.90037 11.91123 11.92239 11.93721
## [137] 11.95514 11.97564 11.99818 12.02220 12.04717 12.07255 12.09778 12.12233
## [145] 12.14565 12.16721 12.19007 12.21721 12.24798 12.28170 12.31772 12.35537
## [153] 12.39400 12.43292 12.47150 12.50905 12.54492 12.57844 12.60895 12.63579
## [161] 12.66043 12.68472 12.70862 12.73206 12.75498 12.77732 12.79902 12.82001
## [169] 12.84025 12.86105 12.88360 12.90761 12.93279 12.95885 12.98552 13.01250
## [177] 13.03950 13.06624 13.09244 13.11780 13.14204 13.16488 13.18602 13.20518
## [185] 13.22207 13.23640 13.24790 13.25627 13.26290 13.26925 13.27509 13.28021
## [193] 13.28440 13.28744 13.28913 13.28924 13.28757 13.28389 13.27800 13.26969
## [201] 13.25874 13.24493 13.22711 13.20482 13.17887 13.15010 13.11932 13.08735
## [209] 13.05503 13.02317 12.99259 12.95984 12.92157 12.87876 12.83238 12.78339
## [217] 12.73276 12.68148 12.63050 12.58081 12.53337 12.48916 12.44914 12.40876
## [225] 12.36374 12.31541 12.26510 12.21416 12.16394 12.11576 12.07096 12.03090
## [233] 11.99252 11.95228 11.91074 11.86846 11.82602 11.78398 11.74290 11.70337
## [241] 11.66593 11.63116 11.59963 11.57190 11.54652 11.52184 11.49809 11.47552
## [249] 11.45435 11.43483 11.41721 11.40171 11.38858 11.37790 11.36950 11.36324
## [257] 11.35900 11.35663 11.35602 11.35701 11.35948 11.36329 11.36831 11.37442
## [265] 11.38146 11.39137 11.40551 11.42292 11.44264 11.46371 11.48517 11.50605
## [273] 11.52539 11.54223 11.55902 11.57860 11.60058 11.62461 11.65031 11.67730
## [281] 11.70521 11.73368 11.76233 11.79079 11.81868 11.84564 11.87130 11.89527
## [289] 11.91890 11.94359 11.96913 11.99526 12.02177 12.04842 12.07497 12.10119
## [297] 12.12684 12.15170 12.17553 12.19809 12.21996 12.24182 12.26362 12.28530
## [305] 12.30682 12.32812 12.34914 12.36985 12.39017 12.41006 12.42947 12.44835
## [313] 12.46664 12.48428 12.50175 12.51943 12.53720 12.55490 12.57240 12.58956
## [321] 12.60624 12.62229 12.63758 12.65231 12.66674 12.68087 12.69466 12.70809
## [329] 12.72115 12.73381 12.74605 12.75784 12.76917 12.78002 12.79036 12.80016
## [337] 12.80951 12.81846 12.82700 12.83512 12.84280 12.85003 12.85680 12.86310
## [345] 12.86892 12.87428 12.87919 12.88366 12.88768 12.89128 12.89445 12.89720
## [353] 12.89954 12.90147 12.90301 12.90415 12.90493 12.90533 12.90536 12.90501
## [361] 12.90426 12.90311 12.90156 12.89958 12.89717
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 365)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.85598 10.91640 10.97579 11.03415 11.09145 11.14770 11.20288 11.25697
##   [9] 11.30997 11.36186 11.41263 11.46227 11.51077 11.55811 11.60429 11.64934
##  [17] 11.69330 11.73619 11.77801 11.81876 11.85845 11.89709 11.93469 11.97125
##  [25] 12.00678 12.04128 12.07476 12.10685 12.13729 12.16626 12.19394 12.22051
##  [33] 12.24614 12.27103 12.29533 12.31925 12.34253 12.36487 12.38628 12.40678
##  [41] 12.42640 12.44517 12.46311 12.48025 12.49660 12.51220 12.52706 12.54122
##  [49] 12.55563 12.57089 12.58649 12.60193 12.61668 12.63025 12.64211 12.65176
##  [57] 12.65870 12.66309 12.66557 12.66632 12.66551 12.66329 12.65984 12.65533
##  [65] 12.64993 12.64381 12.63714 12.63007 12.62279 12.61547 12.60729 12.59752
##  [73] 12.58641 12.57421 12.56117 12.54754 12.53359 12.51956 12.50310 12.48225
##  [81] 12.45785 12.43073 12.40175 12.37172 12.34151 12.31194 12.28385 12.25808
##  [89] 12.23547 12.21686 12.20053 12.18426 12.16813 12.15220 12.13655 12.12124
##  [97] 12.10635 12.09196 12.07813 12.06494 12.05246 12.04076 12.02991 12.01999
## [105] 12.01177 12.00565 12.00111 11.99761 11.99464 11.99166 11.98817 11.98362
## [113] 11.97750 11.97078 11.96472 11.95926 11.95433 11.94988 11.94583 11.94213
## [121] 11.93869 11.93547 11.93240 11.92941 11.92643 11.92437 11.92385 11.92443
## [129] 11.92566 11.92708 11.92824 11.92869 11.92797 11.92564 11.92033 11.91150
## [137] 11.89994 11.88641 11.87172 11.85663 11.84192 11.82837 11.81677 11.80790
## [145] 11.80252 11.80144 11.80241 11.80288 11.80311 11.80334 11.80385 11.80489
## [153] 11.80673 11.80961 11.81381 11.81957 11.82717 11.83686 11.84890 11.86354
## [161] 11.88215 11.90517 11.93154 11.96021 11.99010 12.02015 12.04929 12.07647
## [169] 12.10061 12.12578 12.15630 12.19151 12.23073 12.27329 12.31854 12.36580
## [177] 12.41440 12.46368 12.51296 12.56159 12.60889 12.65419 12.69684 12.73615
## [185] 12.77146 12.80211 12.82742 12.84673 12.86288 12.87895 12.89465 12.90969
## [193] 12.92378 12.93661 12.94790 12.95736 12.96468 12.96958 12.97176 12.97093
## [201] 12.96680 12.95907 12.94529 12.92426 12.89756 12.86678 12.83348 12.79925
## [209] 12.76567 12.73431 12.70676 12.67792 12.64244 12.60155 12.55645 12.50835
## [217] 12.45848 12.40805 12.35827 12.31036 12.26553 12.22499 12.18996 12.15401
## [225] 12.11131 12.06397 12.01411 11.96384 11.91528 11.87054 11.83174 11.80099
## [233] 11.77593 11.75273 11.73131 11.71157 11.69343 11.67682 11.66163 11.64780
## [241] 11.63523 11.62384 11.61354 11.60425 11.59952 11.60176 11.60921 11.62007
## [249] 11.63257 11.64493 11.65536 11.66210 11.66336 11.66198 11.66189 11.66290
## [257] 11.66486 11.66757 11.67088 11.67459 11.67854 11.68255 11.68646 11.69007
## [265] 11.69322 11.69430 11.69244 11.68866 11.68393 11.67924 11.67560 11.67399
## [273] 11.67541 11.68084 11.68911 11.69835 11.70844 11.71929 11.73080 11.74286
## [281] 11.75538 11.76826 11.78141 11.79471 11.80807 11.82139 11.83457 11.84751
## [289] 11.86191 11.87917 11.89874 11.92011 11.94272 11.96604 11.98953 12.01267
## [297] 12.03490 12.05570 12.07452 12.09083 12.10578 12.12081 12.13587 12.15089
## [305] 12.16580 12.18055 12.19507 12.20930 12.22318 12.23663 12.24961 12.26205
## [313] 12.27387 12.28503 12.29521 12.30427 12.31240 12.31979 12.32664 12.33312
## [321] 12.33943 12.34576 12.35230 12.35878 12.36484 12.37050 12.37582 12.38082
## [329] 12.38554 12.39002 12.39429 12.39841 12.40239 12.40629 12.41013 12.41395
## [337] 12.41771 12.42133 12.42479 12.42809 12.43124 12.43422 12.43703 12.43966
## [345] 12.44215 12.44454 12.44681 12.44894 12.45091 12.45270 12.45430 12.45569
## [353] 12.45685 12.45776 12.45841 12.45877 12.45884 12.45863 12.45815 12.45742
## [361] 12.45646 12.45527 12.45387 12.45229 12.45053
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")